<?php

namespace App\Http\Controllers\Api;


use App\Models\CartoonMembers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Validate\api\ScriptValidate;
use App\Models\Script;

class ScriptController extends BaseController
{
    protected $Cartoon;
    protected $v;
    /**
     *
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */

    public function __construct(Script $Script,ScriptValidate $scriptValidate)
    {
        $this->Model = $Script;
        $this->v = $scriptValidate;
    }

    public function checkMember($member_id,$cartoon_id)
    {
        $CartoonMembers=new CartoonMembers();
        $cartoon = $CartoonMembers->where(['member_id'=>$member_id,'cartoon_id'=>$cartoon_id])->value('id');

        return $cartoon;
    }
    /**
     * api获取列表信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList(Request $request)
    {

        $member_id = Auth::guard('api')->user()->member_id;
        $cartoon=$this->checkMember($member_id,$request->cartoon_id);
        if (empty($cartoon)){
            return response()->json(['code'=>0,'msg'=>'当前用户没有权限']);
        }

        $where['cartoon_id']=$request->cartoon_id;


        $user = $this->Model->getList($where,$request->pageSize,'sort','asc');
        $data['total']=$user['total'];
        $data['list']=$user['data'];

        if (empty($user['data'])){
            return response()->json(['code'=>1,'msg'=>'没有数据了','data' => $data]);
        }else{
            return response()->json(['code'=>1,'msg'=>'成功','data' => $data]);
        }

    }

    /**
     * 添加漫画剧本
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addScript(Request $request)
    {

        $member_id = Auth::guard('api')->user()->member_id;
        $data=$request->input();
        $this->v->validate('addScript');
        $cartoon=$this->checkMember($member_id,$request->cartoon_id);
        if (empty($cartoon)){
            return response()->json(['code'=>0,'msg'=>'当前用户没有权限']);
        }
        $rs =$this->Model->create([
            'member_id'=> $member_id,
            'title'=>   $data['title'],
            'subtitle'=> $data['subtitle'],
            'text'=>$data['text'],
            'cartoon_id'=>$data['cartoon_id'],
        ]);
        if ($rs){
            return response()->json(['code'=>1,'msg'=>'创建成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'创建失败']);
        }
    }


    /**
     * 修改漫画剧本
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveScript(Request $request){
        $data = $request->all();

            $up['title']=$data['title'];
            $up['subtitle']=$data['subtitle'];
            $up['text']=$data['text'];
            $up['sort']=(int)$data['sort'];
        $member_id=Auth::user()->member_id;
        $script=$this->Model->shows($data['id']);
        if (empty($script)){
            return response()->json(['code'=>0,'msg'=>'剧本不存在']);
        }
        $cartoon=$this->checkMember($member_id,$script->cartoon_id);
        if (empty($cartoon)){
            return response()->json(['code'=>0,'msg'=>'当前用户没有权限']);
        }
        $rs = $this->Model->where(['member_id'=>$member_id,'id'=>$data['id']])->update($up);
        if ($rs){
            return response()->json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败']);
        }
    }


    /**
     * 删除漫画剧本
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delScript(Request $request){
        $data = $request->all();

        $member_id=Auth::user()->member_id;
//        $member_id=1;
        $script=$this->Model->shows($data['id']);
        if (empty($script)){
            return response()->json(['code'=>0,'msg'=>'剧本不存在']);
        }
        $cartoon=$this->checkMember($member_id,$script->cartoon_id);
        if (empty($cartoon)){
            return response()->json(['code'=>0,'msg'=>'当前用户没有权限']);
        }
        $rs = $this->Model->where(['id'=>$data['id']])->delete();
        if ($rs){
            return response()->json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    /**
     * 漫画剧本详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getScriptInfo(Request $request){
        $data = $request->all();
        $member_id=Auth::user()->member_id;

        $script=$this->Model->shows($data['id']);
        if (empty($script)){
            return response()->json(['code'=>0,'msg'=>'剧本不存在']);
        }
        $cartoon=$this->checkMember($member_id,$script->cartoon_id);
        if (empty($cartoon)){
            return response()->json(['code'=>0,'msg'=>'当前用户没有权限']);
        }

        return response()->json(['code'=>1,'msg'=>'操作成功','data'=>$script]);

    }

}
